﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BahdlClassLib;
using BusinessLayer;

namespace MasterForms
{
    public partial class WorkingHours : CustomFormTemplate
    {
        public WorkingHours()
        {
            InitializeComponent();
        }
        private void WorkingHours_Load(object sender, EventArgs e)
        {
            this.Left = 0;
            this.Top = 0;
            this.Tag = "view";

            dtFromDate.Value = DateTime.Now;
            dtToDate.Value = DateTime.Now;
            txtInTime.Text = string.Empty;
            txtOutTime.Text = string.Empty;
            txtGracePeriod.Text = string.Empty;

            base.ToolBarButtonClick += new ToolBarButtonClickEventHandler(WorkingHours_ToolBarButtonClick);
            base.EnableControls += new EnableControlsEventHandler(WorkingHours_EnableControls);
            base.KeyDown += new KeyEventHandler(WorkingHours_KeyDown);
        }
        void WorkingHours_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                if (this.Tag.ToString() == "view")
                {
                    WorkingHours_ToolBarButtonClick("exit");
                }
                else
                {
                    WorkingHours_ToolBarButtonClick("revert");
                }
            }
        }
        void WorkingHours_EnableControls(bool Enable)
        {
            fraDtl.Enabled = Enable;
        }
        private void WorkingHours_ToolBarButtonClick(string Button)
        {
            if (CommonFunction.InList(Button, "add", "edit", "delete") == true)
            {
                if ((Button == "edit" || Button == "delete"))
                {

                    if (txtInTime.Text == string.Empty || txtOutTime.Text == string.Empty || txtGracePeriod.Text == string.Empty)
                    {
                        return;
                    }
                }
            }
            switch (Button)
            {
                case "exit":
                    if (UIFunction.SureToExit("") == true)
                    {
                        ClearError();
                        this.Close();
                    }
                    break;
                case "revert":
                    ClearError();
                    this.Tag = "view";
                    WorkingHours_EnableControls(false);
                    this.ToolBarEnterEditMode = false;
                    break;
                case "print":

                    break;
                case "find":
                    ClearError();
                    DataSet dsLocal = new DataSet();
                    Popup popup = new Popup();
                    try
                    {
                        ClsWorkingHours ClsWorkingHours = new ClsWorkingHours();
                        dsLocal.Tables.Add(ClsWorkingHours.GetAllWorkingHours());
                        dsLocal.Tables[0].TableName = "popup";
                        popup.Width = 65;
                        popup.ShowPopup(null, "WorkingHoursID", ref dsLocal, "popup", "DateFrom,DateTo,StartWorkingTime,OffWorkingTime,MinimumWorkingHour", "", "Manage Working Hours", "From Date,To Date,Start Working Time,Off Working Time,Minimum Working Hour", "10,10,13,13,15");
                        if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
                        {
                            uiGetData(Convert.ToInt32(popup.Field("WorkingHoursID")));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    finally
                    {
                        dsLocal = null;
                        popup = null;
                    }
                    break;
                case "add":
                    ClearError();
                    if (uiGetData(0))
                    {
                        this.ToolBarEnterEditMode = true;
                        this.Tag = "add";
                        WorkingHours_EnableControls(true);

                        dtFromDate.Select();
                        dtToDate.Select();
                        txtInTime.Select();
                        txtOutTime.Select();
                        txtGracePeriod.Select();
                    }
                    break;
                case "edit":
                    ClearError();
                    this.ToolBarEnterEditMode = true;
                    this.Tag = "edit";
                    WorkingHours_EnableControls(true);
                    dtFromDate.Select();
                    dtToDate.Select();
                    txtInTime.Select();
                    txtOutTime.Select();
                    txtGracePeriod.Select();
                    break;
                case "save":
                    if (!ValidateName())
                        return;
                    if (uiPutData() == true & this.Tag.ToString() == "edit")
                    {
                        this.Tag = "view";
                        this.ToolBarEnterEditMode = false;
                        WorkingHours_EnableControls(false);
                    }
                    break;
                case "delete":
                    ClearError();
                    uiDeleteData();
                    break;
            }


        }

        private void uiDeleteData()
        {
            try
            {
                ClsWorkingHours ClsWorkingHours = new ClsWorkingHours();
                ClsWorkingHours.WorkingHoursID = Convert.ToInt32(txtInTime.Tag);
                ClsWorkingHours.DeleteWorkingHour();
                uiGetData(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
        }
        private bool uiPutData()
        {
            bool functionReturnValue = false;
            try
            {
                ClsWorkingHours ClsWorkingHours = new ClsWorkingHours();

                ClsWorkingHours.DateFrom = dtFromDate.Value;
                ClsWorkingHours.DateTo = dtToDate.Value;
                ClsWorkingHours.StartWorkingTime = Convert.ToDateTime(txtInTime.Text);

                if (txtInTime.Tag.ToString() != string.Empty)
                    ClsWorkingHours.WorkingHoursID = Convert.ToInt32(txtInTime.Tag);


                ClsWorkingHours.OffWorkingTime = Convert.ToDateTime(txtOutTime.Text);
                ClsWorkingHours.MinimumWorkingHour = Convert.ToInt32(txtGracePeriod.Text);



                if (ClsWorkingHours.SaveWorkingHour(this.Tag.ToString()))
                    functionReturnValue = true;
                uiGetData(0);
                if (this.Tag.ToString() == "add")
                {
                    dtFromDate.Select();
                    dtToDate.Select();
                    txtInTime.Select();
                    txtOutTime.Select();
                    txtGracePeriod.Select();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }
        private bool uiGetData(int KeyField)
        {
            bool functionReturnValue = false;
            try
            {
                ClsWorkingHours ClsWorkingHours = new ClsWorkingHours();
                ClsWorkingHours.GetWorkingHour(KeyField);


                if (KeyField == 0)
                {

                    dtFromDate.Value = DateTime.Now;
                    dtToDate.Value = DateTime.Now;
                    txtInTime.Text = string.Empty;
                    txtInTime.Tag = string.Empty;
                    txtOutTime.Text = string.Empty;
                    txtGracePeriod.Text = string.Empty;
                }
                else
                {

                    dtFromDate.Value = ClsWorkingHours.DateFrom;
                    dtToDate.Value = ClsWorkingHours.DateTo;
                    txtInTime.Text = Convert.ToDateTime(ClsWorkingHours.StartWorkingTime).ToString();
                    txtInTime.Tag = ClsWorkingHours.WorkingHoursID.ToString();

                    txtOutTime.Text = Convert.ToDateTime(ClsWorkingHours.OffWorkingTime).ToString();
                    txtGracePeriod.Text = Convert.ToInt32(ClsWorkingHours.MinimumWorkingHour).ToString();
                }



                functionReturnValue = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }

        private bool ValidateName()
        {
            bool bStatus = true;
            if (txtGracePeriod.Text == "")
            {
                errorProvider1.SetError(txtGracePeriod, "Please enter the Grace Period");
                return false;
            }
            ClearError();
            return bStatus;
        }
        private void ClearError()
        {
            errorProvider1.Clear();
        }

        private void dtFromDate_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void dtToDate_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtInTime_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtOutTime_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        

    }

}

